<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="style.css">
<script src="jquery-1.11.0.js"></script>
		<script src="gen.js"></script>	
		<script>
			
			//makes a synchronous call to the page u and return the 
			//result as object
			var x;
			function syncAjax(u){
				var obj=$.ajax(
					{url:u,
					 async:false
					 }
				);
				return $.parseJSON(obj.responseText);
				
			}
			function get_schools(id){
                x = id;
				var u="school_action.php?cmd=1&id="+id;
				
				return syncAjax(u);
			}
			
			function edit(obj,id){
				var r=get_schools(id);
				
				if(r.result==0){
					//show error message
					return;
					
				}
				
				//get the data from object r and put it in the form
				
			
				
				$("#school_id").prop("value", r.school.school_id);
				$("#school_name").prop("value", r.school.school_name);
				$("#sch_notes").prop("value", r.school.sch_notes);
				$("#community_id").prop("value", r.school.community_id);
				
				
				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				//display the form 
				$("#divEdit").fadeIn(1000);
			}
            function add(obj)
            {
                $("#school_name").prop("value", "");
                $("#sch_notes").prop("value", "");
                $("#community_id").prop("value", "");

                var y= event.clientY;
                var x= event.clientX/2;
                //use x and y to set the location of the form
                $("#divAdd").css("top",y);
                $("#divAdd").css("left",x);
                //display the form
                $("#divAdd").fadeIn(500);

            }
			
			//returns a result object for one vaccine 
			
			
			//makes asynchronous call to the save page
			
			function save()
            {
				//complete the url
				var sid = document.getElementById("school_id").value;	
				var sn = document.getElementById("school_name").value;
				var schn = document.getElementById("sch_notes").value;
				var ci = document.getElementById("community_id").value;
				//var id =5;
				var u="school_action.php?cmd=3&sid="+sid
				                  +"&sn="+sn
								  +"&schn="+schn
								  +"&ci="+ci;
						//fade out the form in half a second
				$("#divEdit").css("background-color","GG0000");
				$("#divEdit").fadeOut(900); // hides any html object
				alert("saved");
				location.reload();
						return syncAjax(u);
			}

            function delete_sl(obj,id)
            {
                var r=get_schools(id);
                // alert(r);
                if(r.result==0){
                    //show error message
                    return;
                }

                //get the data from the object r and put it in the form
                $("#school_id").prop("value", r.school.school_id);
                $("#sch_name").prop("value", r.school.school_name);
                $("#notes").prop("value", r.school.sch_notes);
                //show the form
                //find where the user clicked and store it in x and y
                var y=event.clientY;
                var x=event.clientX/2;
                //use x and y to set the location of the form
                $("#divDelete").css("top",y);
                $("#divDelete").css("left",x);
                //display the form
                $("#divDelete").fadeIn(300);
            }

            function delete_sch()
            {

                var u="school_action.php?cmd=5&sid="+x;
                alert(u);
                $("#divDelete").css("background-color","006600");
                $("#divDelete").fadeOut(500); // hides any html object
                alert("Deleted");
                location.reload();

                return syncAjax(u);

            }

            function save_add()
            {
                //complete the url
                var sna=$("#s_name").val();
                var snn=$("#s_notes").val();
                var cd=$("#s_community").val();
                var u="school_action.php?cmd=4&sna="+sna+"&snn="+snn+"&cd="+cd;
                alert(u);

                $.getJSON(u,saveDone);
                //reload
                window.location.reload();
                cancel_add();
            }
			
			function saveDone(data){
				
				alert(data);
			}
            function cancel_add()
            {
                $("#divAdd").fadeOut(500);
            }
			//hides the form
			function cancel(){
				//fade out the form in half a second
				$("#divEdit").css("background-color","GG0000");
				$("#divEdit").fadeOut(500); // hides any html object
			}

            function cancel_sch()
            {
                //fade out the form in half a second
                $("#divDelete").css("background-color","006600");
                $("#divDelete").fadeOut(500); // hides any html object
            }
		</script>		
			
	</head>
	<body>
		<table>
			<tr>
				<td colspan="2" id="pageheader">
					<h3>Health Information System</h3>
				</td>
			</tr>
			<tr>
				<td id="mainnav">
					<div class="menuitem">location</div>
					<div class="menuitem">opd cases</div>
					<div class="menuitem">health promotion</div>
					<div class="menuitem">nutrition</div>
					<div class="menuitem">child welfare</div>
					<div class="menuitem"><a href = 'view.php'>family planning</a></div>
				</td>
				
				<td id="content">
					<div id="divPageMenu">
						<span class="menuitem" >sub districts</span> |
						<span class="menuitem" >communities</span> |
						<span class="menuitem" >view map</span> |
						<input type="text" id="txtSearch">
						<span class="menuitem">search</span> |
					</div>
							<div id="divStatus" class="status">
						status message
					</div>
                    <div id="divPageMenu">
                        <span class="menuitem"><a href="school_view.php" >Schools Visits</a></span> |
                        <span class="menuitem"><a href="household_view.php">Households Visits</a></span> |
                        <span class="menuitem"><a href="community_members_view.php">Community Members</a></span> |
                        <span class="menuitem"><a href="community_list.php">Communities</a></span> |
                        <span class="menuitem"><a href="service_view.php">Family Planning Service</a></span>
                    </div>
					<div id="divContent">
                        <b><p align = "center">SCHOOL VISITS</p></b>
                        <span class='hotspot' onclick='add(this)'>Add</span>
						<table class="reportTable" width="100%" >
                            <tr class ="header"></tr>
							<tr class="header" >
								<td>ID</td>
								<td>School Name</td>
								<td>Notes</td>
                                <td></td>
                                <td></td>
							</tr>
							</div>
					<div id="divStatus" class="status">
						
					</div>
					<div id="divContent"> 					
<?php	
	// including the schools class for method calls
			include_once("schools.php");

			//call methods here to test
			$obj =  new schools();
			if (!$obj->get_all_schools())
			{
				echo "error";
				exit();
			}


	$row=$obj->fetch();
	$row_counter=0;
	while($row){
		
		if($row_counter%2==0)
        {
			$style=" class='row1' ";
		}
        else
        {
			$style=" class='row2'  ";
		}	
		$id=$row['school_id'];
		echo "<tr $style >";
			echo "<td>$id</td>";
			echo "<td>$row[school_name]</td>";
            echo "<td>$row[sch_notes]</td>";
			echo "<td><span class='hotspot' onclick='edit(this,$id)'>edit<span></td>";
        echo "<td><span class='hotspot' onclick='delete_sl(this,$id)'>delete<span></td>";
		echo "</tr>";
		$row=$obj->fetch();
		$row_counter++;
	}
?>
   </table>
    </div>
	</td>
	</tr>
		</table>
	<div id="divEdit" class = "popupForm">
		<table class="tableForm" >
		<tr>
			    <td class="label">school_name:</td>
				<td class="field"><input type="text" value="<?php echo $row['school_name'] ?>" id="school_name" ></td>
			</tr>
			<tr>
				<td class="label">sch_notes</td> 
				<td class="field"><input type="text" value="" id="sch_notes" >
				</td>
			</tr>
			<tr>
				<td class="label">community_id </td> 
				<td class="field"><input type="text" value="" id="community_id" >
				</td>
			</tr>
			<tr>
			  <td class="label"></td>
			  <td class="field">
			  <input type="button" value="save" onclick="save()" >
			  <input type="button" value="cancel" onclick="cancel()" >
			</td>
			</tr>
		    <input type="hidden" value="<?php echo $row['school_id'] ?>" id ="school_id" > 
			</table>
	     </div>
        <div id="divAdd" class="popupForm">
            <table class="tableForm" >
                <tr>
                    <td class="label">School:</td>
                    <td class="field"><input type="text" value="" id="s_name" ></td>
                </tr>
                <tr>
                    <td class="label">Notes:</td>
                    <td class="field"><input type="text" value="" id="s_notes" >
                    </td>
                </tr>
                <tr>
                    <td class="label">Community_id:</td>
                    <td class="field"><input type="text" value="" id="s_community" >
                    </td>
                </tr>
                <tr>
                    <td class="label"></td>
                    <td class="label">
                        <input type="button" value="save" onclick="save_add()" >
                        <input type="button" value="cancel" onclick="cancel_add()" >
                    </td>
                </tr>
            </table>
        </div>

        <div id="divDelete" class = "popupForm">

            <table class="tableForm" >
                <tr>
                    <div class="label">Are you sure you want to delete?</div>
                    <td class="label">School</td>
                    <td class="field"><input type="text" value="<?php echo $row['school_name'] ?>" id="sch_name" ></td>
                </tr>
                <tr>
                    <td class="label">Notes</td>
                    <td class="field"><input type="text" value="" id="notes" >
                    </td>
                </tr>
                <tr><td class="label"></td>
                    <td class="field">
                        <input type="button" value="Yes" onclick="delete_sch()" >
                        <input type="button" value="No" onclick="cancel_sch()" >
                    </td>
                </tr>
                <input type="hidden" value="<?php echo $row['school_id'] ?>" id ="school_id" >

            </table>

        </div>

			
	</body>
</html>	